#pragma once

#include "channel.h"
#include "event_loop.h"
#include "inet_address.h"
#include "noncopyable.h"
#include "socket.h"

#include <functional>

class EventLoop;
class InetAddress;

class Acceptor : noncopyable {
  public:
    using NewConnectionCallback = std::function<void(int sockfd, const InetAddress &)>;
    Acceptor(EventLoop *loop, const InetAddress &listen_addr, bool reuseport);
    ~Acceptor();

    void set_new_connection_callback(const NewConnectionCallback &cb) {
        new_connection_callback_ = cb;
    }

    bool listenning() const { return listenning_; }
    void listen();

  private:
    void handle_read();
    EventLoop *loop_; // Acceptor用的就是用户定义的base_loop,也程作main_loop
    Socket accept_socket_;
    Channel accept_channel_;
    NewConnectionCallback new_connection_callback_;
    bool listenning_;
};